﻿using System.Collections.Generic;
using System;

namespace Ex03.GarageLogic
{
    public abstract class Car : Vehicle
    {
        public enum eColor
        { 
            Red,
            Yellow,
            Black,
            White,
            Blue
        }

        public enum eNumberOfDoors
        {
            Two = 2,
            Three,
            Four,
            Five
        }

        private const string k_ColorKey = "Color";
        private const string k_NumberofDoorsKey = "Number of Doors";
        private const int k_NumberOfWheels = 4;
        private const float k_MaximumAirPressure = 31.0f;

        private eColor m_Color;
        private eNumberOfDoors m_NumberOfDoors;

        protected Car()
        {
            Color = eColor.White.ToString();
            NumberOfDoors = eNumberOfDoors.Two.ToString();
        }

        public override void ParseFields(Dictionary<string, string> i_Form)
        {
            base.ParseFields(i_Form);
            string colorStr;
            if (!i_Form.TryGetValue(k_ColorKey, out colorStr))
            {
                throw new KeyNotFoundException(k_ColorKey);
            }

            string numberOfDoorsStr;
            if (!i_Form.TryGetValue(k_NumberofDoorsKey, out numberOfDoorsStr))
            {
                throw new KeyNotFoundException(k_NumberofDoorsKey);
            }

            Color = colorStr;
            NumberOfDoors = numberOfDoorsStr;
            Wheels = ParseWheels(i_Form, k_NumberOfWheels, k_MaximumAirPressure);
        }

        public override Dictionary<string, string> CreateForm()
        {
            Dictionary<string, string> form = base.CreateForm();

            form.Add(k_ColorKey, null);
            form.Add(k_NumberofDoorsKey, null);
            Wheel.CreateForm(ref form, k_NumberOfWheels);

            return form;
        }

        public string Color
        {
            get { return m_Color.ToString(); }
            set 
            {
                if (!Enum.IsDefined(typeof(eColor), value))
                {
                    throw new FormatException(k_ColorKey);
                }

                try
                {
                    m_Color = (eColor)System.Enum.Parse(typeof(eColor), value);
                }
                catch (System.ArgumentException)
                {
                    throw new System.ArgumentException(k_ColorKey);
                }
            }
        }

        public string NumberOfDoors
        {
            get { return m_NumberOfDoors.ToString(); }
            set 
            { 
                if(!Enum.IsDefined(typeof(eNumberOfDoors), value))
                {
                    throw new FormatException(k_NumberofDoorsKey);
                }

               try
               {
                   m_NumberOfDoors = (eNumberOfDoors)System.Enum.Parse(typeof(eNumberOfDoors), value);
               }
               catch (System.ArgumentException)
               {
                   throw new System.ArgumentException(k_NumberofDoorsKey);
               }
            } 
        }

        public override string ToString()
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            stringBuilder.Append(base.ToString());
            stringBuilder.Append("Color: ");
            stringBuilder.AppendLine(System.Enum.GetName(typeof(eColor), m_Color).ToString());
            stringBuilder.Append("Number Of Doors: ");
            stringBuilder.AppendLine(System.Enum.GetName(typeof(eNumberOfDoors), m_NumberOfDoors).ToString());

            return stringBuilder.ToString();
        }
    }
}